home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / console / splitvt-.000 / splitvt- / splitvt-1.6.1 / vttest.c < prev   
Encoding:
C/C++ Source or Header  |  1994-12-05  |  2.9 KB  |  115 lines

  1. /* vttest.c -- written by Charles Howes (chowes@sfu.ca) */
  2.  
  3. /* Modified to (hopefully) be more portable, run as a function,
  4.    and test only for vt100 terminal emulation on 9/30/93
  5.  
  6.    This function will return 1 if the tty is vt100 or 0 if an error
  7.    occurred or the terminal doesn't appear to be a vt100 terminal.
  8.  
  9.    Completely re-written 10/9/93 for better portability
  10.  
  11.  
  12.     -Sam Lantinga        (slouken@toadflax.cs.ucdavis.edu)
  13. */
  14.  
  15. #include    <sys/types.h>
  16. #include    <stdio.h>
  17. #include    <fcntl.h>
  18. #ifdef HAVE_TERMIO_H 
  19. #include        <termio.h> 
  20. #else 
  21. #include    <setjmp.h> 
  22. #include    <signal.h> 
  23. #include    <sys/ioctl.h> 
  24. #define termio    sgttyb 
  25. #define TCGETA    TIOCGETP
  26. #define TCSETAW    TIOCSETP
  27. #endif /* HAVE_TERMIO_H */
  28.  
  29. #ifdef NEED_COMPAT_H
  30. #include    <sys/ioctl_compat.h>
  31. #endif /* NEED_COMPAT_H */
  32.  
  33. #ifdef HAVE_BSDTTY_H
  34. #include        <sys/bsdtty.h>
  35. #endif /* HAVE_BSDTTY_H */
  36.  
  37. #ifdef MAIN
  38. int main() { if ( vttest() ) printf("vt100\n"); }
  39. #endif
  40.  
  41. /* Register that we are alarmed. (called by SIG_ALRM on BSD) */
  42. static int alarmed;
  43. #ifndef HAVE_TERMIO_H
  44. static jmp_buf alarm_buf;
  45. static void alrm_trap() { alarmed=1; longjmp(alarm_buf, 1); }
  46. #endif /* No termio.h */
  47.  
  48. int vttest() 
  49. {
  50.     char   buff[512];
  51.     int x=0, w, rc=0, fd;
  52.     struct termio ttold, ttraw;
  53.  
  54.     /* Set the terminal in a raw mode */
  55.     if ( (fd=open("/dev/tty", O_RDWR, 0666)) < 0 )
  56.         return(0);
  57.  
  58.     if ( ioctl(fd, TCGETA, (char *)&ttold) < 0 )
  59.         return(0);
  60.     (void) ioctl(fd, TCGETA, (char *)&ttraw);
  61.  
  62. #ifdef HAVE_TERMIO_H
  63. #ifdef SEVEN_BIT
  64.         ttraw.c_iflag=(IGNBRK | ISTRIP);   /* turn off all input control */
  65. #else
  66.     ttraw.c_iflag=(IGNBRK);   /* turn off all input control */
  67. #endif /* SEVEN_BIT */
  68.         ttraw.c_oflag &= ~(OLCUC | ONLCR | OCRNL | ONLRET);
  69.                                         /* disable output post-processing */
  70.         ttraw.c_lflag = 0;
  71.         ttraw.c_cc[VMIN]=0;          /* 1 or more chars satisfy read */
  72.         ttraw.c_cc[VTIME]=10;        /* 10'ths of seconds between chars */
  73. #else
  74.     ttraw.sg_flags |= RAW;        /* turn RAW mode on */
  75.     ttraw.sg_flags &= ~ECHO;    /* turn ECHO off */
  76. #endif /* HAVE_TERMIO_H */
  77.  
  78.         if (ioctl(fd, TCSETAW, (char *)&ttraw) < 0)
  79.                 return(0);
  80.  
  81.       write(fd,"\033[c", 3);    /* Vt100 test: ESC [ c */
  82.  
  83. #ifndef HAVE_TERMIO_H        /* We need to set an alarm */
  84.     signal(SIGALRM, alrm_trap);
  85.     alarmed=0;
  86.     alarm(1);
  87.     setjmp(alarm_buf);
  88. #endif
  89.     while ( !alarmed && (x < 20) ) { 
  90.         if ( read(fd, &buff[x++], 1) <= 0 )
  91.             break;
  92.     }
  93.     buff[x]='\0';        /* For printing, if we desire. */
  94.       if ( buff[0] == '\033' )    /* An escape sequence? :) */
  95.         rc=1;
  96.  
  97. #ifndef HAVE_TERMIO_H
  98.     alarm(0);
  99.     signal(SIGALRM, SIG_DFL);
  100. #endif
  101.         (void) ioctl(fd, TCSETAW, (char *)&ttold);
  102.     (void) close(fd);
  103.  
  104. #ifdef not_defined        /* Print out the response for debugging */
  105.     for ( w=0; buff[w]; ++w ) 
  106.         if ( buff[w] < ' ' ) 
  107.             printf("^%c", buff[w]+'@');
  108.         else
  109.             printf("%c", buff[w]);
  110.     printf("\n");
  111. #endif /* not_defined */
  112.  
  113.     return rc;
  114. }
  115.